﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Administration/Views/Shared/Administration.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MotorcycleStore.DomainModel.Category>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

	<script type="text/javascript">
		$(function () {
			$("#category-dialog").dialog({
				autoOpen: false,
				buttons: {
					"Create": function () {
						$.ajax({
							data: $("#category-form").serialize(),
							dataType: "html",
							error: ajax_err_handler,
							success: function (data) {
								$("#category-list").html(data);
								register_list_clicks();
							},
							type: "POST",
							url: '<%: Url.Action("Create") %>'
						});
						$(this).dialog("close");
					}
				},
				modal: true
			});

			$("#open-category-dialog").button({
				icons: {
					primary: 'ui-icon-newwin'
				}
			}).click(function () {
				$("#category-dialog").dialog("open");
				return false;
			});

			register_list_clicks();
		});

		function register_list_clicks() {
			$("a.category-delete").button({
				icons: {
					primary: 'ui-icon-trash'
				}
			}).click(function () {
				var row = $(this).parents("tr");
				if (confirm("Deleting " + $(this).parent().next().text() + " and associated motorcycles, are you sure?")) {
					$.ajax({
						error: ajax_err_handler,
						success: function () {
							row.fadeOut();
							row.remove();
						},
						type: 'POST',
						url: $(this).attr("href")
					});
				}
				return false;
			});

			$("a.category-edit").button({
				icons: {
					primary: 'ui-icon-wrench'
				}
			});

			$("a.category-detail").button({
				icons: {
					primary: 'ui-icon-tag'
				}
			});
		}

		function ajax_err_handler(xhr, statusTxt, errThrown) {
			alert("ERROR\n\n" + xhr.responseText);
		}
	</script>

    <h2>Category Index</h2>

	<div id="category-list">
		<% Html.RenderPartial("CategoryList"); %>
	</div>

    <p>
        <%: Html.ActionLink("New Category", "Create", null, new { id = "open-category-dialog" }) %>
    </p>

	<div id="category-dialog" title="New Category" class="ui-helper-hidden">
		<form id="category-form" action="<%: Url.Action("Create") %>">
			<% Html.RenderPartial("CategoryForm", new MotorcycleStore.DomainModel.Category()); %>
		</form>
	</div>

</asp:Content>

